package com.swak.exception;

import javax.naming.LimitExceededException;

/**
 * RpcException.
 * 
 * @author lifeng
 */
public final class RpcException extends RuntimeException {

	public static final int UNKNOWN_EXCEPTION = 0;
	public static final int NETWORK_EXCEPTION = 1;
	public static final int TIMEOUT_EXCEPTION = 2;
	public static final int BIZ_EXCEPTION = 3;
	public static final int FORBIDDEN_EXCEPTION = 4;
	public static final int SERIALIZATION_EXCEPTION = 5;
	public static final int NO_INVOKER_AVAILABLE_AFTER_FILTER = 6;
	public static final int LIMIT_EXCEEDED_EXCEPTION = 7;
	private static final long serialVersionUID = 1L;
	private final int code;

	public RpcException(int code, String message, Throwable cause) {
		super(message, cause);
		this.code = code;
	}

	public RpcException(int code, String message) {
		super(message);
		this.code = code;
	}

	public RpcException(int code, Throwable cause) {
		super(cause);
		this.code = code;
	}

	public boolean isBiz() {
		return code == BIZ_EXCEPTION;
	}

	public boolean isForbidded() {
		return code == FORBIDDEN_EXCEPTION;
	}

	public boolean isTimeout() {
		return code == TIMEOUT_EXCEPTION;
	}

	public boolean isNetwork() {
		return code == NETWORK_EXCEPTION;
	}

	public boolean isSerialization() {
		return code == SERIALIZATION_EXCEPTION;
	}

	public boolean isNoInvokerAvailableAfterFilter() {
		return code == NO_INVOKER_AVAILABLE_AFTER_FILTER;
	}

	public boolean isLimitExceed() {
		return code == LIMIT_EXCEEDED_EXCEPTION || getCause() instanceof LimitExceededException;
	}
}
